package vma.nicici.chat.history;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbOpenHelper extends SQLiteOpenHelper {
	
	private static final String LOGTAG = "DatabaseHelper";
	
	public static final int DATABASE_VERSION = 1; //toto treba zmenit, ak zmenime schemu
	public static final String DATABASE_NAME = "History.db";	

	private static final String TEXT_TYPE = " TEXT";
	private static final String INTEGER_TYPE = " INTEGER";
	private static final String TIMESTAMP_TYPE = " TEXT"; // ci INTEGER/REAL
	private static final String REAL_TYPE = " REAL";
	private static final String COMMA_SEP = ", ";
	
	private static final String SQL_CREATE_USERS =
					"CREATE TABLE IF NOT EXISTS " + Contract.UsersEntry.TABLE_NAME + " (" +
					Contract.UsersEntry._ID + INTEGER_TYPE + " PRIMARY KEY," +
					Contract.UsersEntry.COLUMN_NAME_FB_USER_ID + INTEGER_TYPE + COMMA_SEP +
					Contract.UsersEntry.COLUMN_NAME_FIRST_NAME + TEXT_TYPE + COMMA_SEP +
					Contract.UsersEntry.COLUMN_NAME_LAST_NAME + TEXT_TYPE + 
					 " )";
	
	private static final String SQL_DELETE_USERS = 
			"DROP TABLE IF EXISTS " +Contract.UsersEntry.TABLE_NAME;
	
	private static final String SQL_CREATE_HISTORY = 
					"CREATE TABLE IF NOT EXISTS " + Contract.HistoryEntry.TABLE_NAME + " (" +
					Contract.HistoryEntry._ID + INTEGER_TYPE + " PRIMARY KEY," +
					Contract.HistoryEntry.COLUMN_NAME_FB_USER_ID + INTEGER_TYPE + COMMA_SEP +
					Contract.HistoryEntry.COLUMN_NAME_TIMESTAMP + TIMESTAMP_TYPE + COMMA_SEP +
					Contract.HistoryEntry.COLUMN_NAME_MESSAGE_TEXT + TEXT_TYPE + COMMA_SEP +
					Contract.HistoryEntry.COLUMN_NAME_LATITUDE + REAL_TYPE + COMMA_SEP +
					Contract.HistoryEntry.COLUMN_NAME_LONGITUDE + REAL_TYPE +
					" )";
	

	private static final String SQL_DELETE_HISTORY =
			"DROP TABLE IF EXISTS " + Contract.HistoryEntry.TABLE_NAME;
		
	
	public DbOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);		
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(SQL_CREATE_USERS);
		db.execSQL(SQL_CREATE_HISTORY);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.d(LOGTAG,"on upgrade from " + oldVersion + " to " + newVersion);
		db.execSQL(SQL_DELETE_HISTORY);
		db.execSQL(SQL_DELETE_USERS);
		onCreate(db);
	}
			
	
					
			
}